package com.fescoservice.config;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

public class LocaleManager {
  static Properties prop;
  Hashtable ht;
  static Object lock = new Object();
  static final Logger log = Logger.getLogger("portal");
  Locale defaultLocale;

  protected Locale getLocaleFromLanguage(String lang) {
    int s=0;
    int d=0;
    Locale locale=null;
    if ((s=lang.indexOf(";"))<0) lang=lang.trim();
    else  lang=lang.substring(0,s).trim();
    if ((d=lang.indexOf("-"))<0) locale=new Locale(lang,"");
    else locale=new Locale(lang.substring(0,d),lang.substring(d+1));
    return locale;
  }

  public Locale getDefaultLocale() {
    return defaultLocale;
  }

  public String getDefaultCharSet() {
    return (String)prop.get("DefaultCharSet");
  }

  public String getDefaultCharSet(String siteCode) {
    String charSet=(String)prop.get(siteCode+".charset");
    if (charSet==null) charSet=getDefaultCharSet();
    return charSet;
  }

  public Locale getDefaultLocale(String siteCode) {
    Locale locale=null;
    String language=(String)prop.get(siteCode+".language");
    String country=(String)prop.get(siteCode+".country");
    String variant=(String)prop.get(siteCode+".variant");
    if (language==null || country==null) locale=getDefaultLocale();
    else locale=new Locale(language,country,variant);
    return locale;
  }

  public ResourceBundle getBundle(String bundleName) {
    return getBundle(bundleName,getDefaultLocale());
  }

  public ResourceBundle getBundle(String bundleName,String languages) {
    ResourceBundle rb=null;
    StringTokenizer st=new StringTokenizer(languages,",");
    while (st.hasMoreTokens()) {
      Locale locale=getLocaleFromLanguage(st.nextToken());
      locale.setDefault(defaultLocale);
      if ((rb=ResourceBundle.getBundle(bundleName,locale))!=null) break;
    }
    if (rb==null) rb=getBundle(bundleName);
    return rb;
  }

  public ResourceBundle getBundle(String bundleName,Locale locale) {
    if (locale==null) locale=getDefaultLocale();
    ResourceBundle rb;
    rb=(ResourceBundle)ht.get(bundleName+locale.toString());

    if (rb==null) {
      locale.setDefault(defaultLocale);
      rb=ResourceBundle.getBundle(bundleName,locale);
      if (rb.getKeys().hasMoreElements()) {
        ht.put(bundleName+locale.toString(),rb);
      }
      else {
        rb=ResourceBundle.getBundle(bundleName,getDefaultLocale());
      }
    }
    return rb;
  }

  public LocaleManager() {
    if (ht==null) ht=new Hashtable();
    try {
      if(prop==null) {
        Properties pp = new Properties();
        pp.load(getClass().getResourceAsStream("eplocale.properties"));
        synchronized(lock) {
          prop=pp;
        }
      }
      String language=(String)prop.get("DefaultLanguage");
      String country=(String)prop.get("DefaultCountry");
      String variant=(String)prop.get("DefaultVariant");
      defaultLocale=new Locale(language,country,variant);
    }
    catch (IOException e) {
      System.out.println(e.getMessage());
      System.out.println("Can't find default locale");
    }
  }

  public LocaleManager(String pageName) {
    ht=new Hashtable();
    try {
      prop=new Properties();
      prop.load(getClass().getResourceAsStream("eplocale.properties"));
      String language=(String)prop.get("DefaultLanguage."+pageName);
      String country=(String)prop.get("DefaultCountry."+pageName);
      String variant=(String)prop.get("DefaultVariant."+pageName);
      defaultLocale=new Locale(language,country,variant);
    }
    catch (IOException e) {
      System.out.println(e.getMessage());
      System.out.println("Can't find default locale");
    }
  }
}